<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="打卡类别" prop="chName">
        <!--        <el-input-->
        <!--          v-model="queryParams.chName"-->
        <!--          placeholder="请输入类别名称"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <el-select v-model="queryParams.chName" clearable placeholder="请选择类别名称">
          <el-option v-for="item in type_list" :key="item.id" :value="item.name" :label="item.name"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="infoContent">
        <el-input
          v-model="queryParams.infoContent"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="权限" prop="agentLevel">
        <el-select v-model="queryParams.agentLevel" placeholder="请选择权限">
          <el-option
            v-for="dict in dict.type.agent_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="isState">
        <el-select v-model="queryParams.isState" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.check_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="params">
        <el-date-picker
          v-model="dataPicker"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aCheckInfor:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aCheckInfor:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aCheckInfor:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aCheckInforList" @selection-change="handleSelectionChange" :key="updateTable">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="infoContent"/>
      <el-table-column label="图片展示" align="center" prop="infoImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.infoImage" :showImgNum="3" :width="50" :height="50"/>
        </template>
      </el-table-column>
      infoContent
      <el-table-column label="详情" align="center" prop="infoDetails">
        <template slot-scope="scope">
          <div v-html="scope.row.infoDetails" v-sub="30" v-copy></div>
        </template>
      </el-table-column>
      <el-table-column label="类别名称" align="center" prop="chName"/>

      <!--      <el-table-column label="点赞数" align="center" prop="likeNum" />-->
      <el-table-column label="下载数" align="center" prop="downloadNum"/>
      <el-table-column label="收藏数" align="center" prop="collectionNum"/>
      <!--      <el-table-column label="分享数" align="center" prop="shareNum" />-->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isState">
        <template slot-scope="scope">
          <!--          <div v-if="scope.row.">-->
          <dict-tag :options="dict.type.check_state" :value="scope.row.isState"/>
        </template>
      </el-table-column>
      <el-table-column label="权限" align="center" prop="agentLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agent_level" :value="scope.row.agentLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="是否上线" align="center" prop="isShow" width="140">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isShow" @change="handleSwitchChange(scope.row)" active-text="上线"
                     inactive-text="下线"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="numSoft"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aCheckInfor:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aCheckInfor:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打卡详细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="infoContent">
          <el-input v-model="form.infoContent" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="类别" prop="checkId">
          <!--          <el-input v-model="form.infoContent" placeholder="请输入标题" />-->
          <el-select v-model="form.checkId" clearable placeholder="请选择类别">
            <el-option v-for="item in type_list" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片展示" prop="infoImage">
          <image-upload v-model="form.infoImage" :file-type="['jpg','png','jpeg','mp4']"/>
        </el-form-item>
        <el-form-item label="详情" prop="infoDetails">
          <editor v-model="form.infoDetails" :min-height="192"/>
        </el-form-item>
        <!--        <el-form-item label="点赞数" prop="likeNum">-->
        <!--          <el-input v-model="form.likeNum" placeholder="请输入点赞数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="下载数" prop="downloadNum">-->
        <!--          <el-input v-model="form.downloadNum" placeholder="请输入下载数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="收藏数" prop="collectionNum">-->
        <!--          <el-input v-model="form.collectionNum" placeholder="请输入收藏数" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="分享数" prop="shareNum">-->
        <!--          <el-input v-model="form.shareNum" placeholder="请输入分享数" />-->
        <!--        </el-form-item>-->
        <el-form-item label="审核状态" prop="isState" v-if="form.id">
          <el-select v-model="form.isState" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.check_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限" prop="agentLevel">
          <el-select v-model="form.agentLevel" placeholder="请选择权限">
            <el-option
              v-for="dict in dict.type.agent_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否上线" prop="isShow">
          <el-switch v-model="form.isShow" active-text="上线" inactive-text="下线"/>
        </el-form-item>
        <el-form-item label="排序" prop="numSoft">
          <el-input-number v-model="form.numSoft" :min="0" :max="10000" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listACheckInfor,
  getACheckInfor,
  delACheckInfor,
  addACheckInfor,
  updateACheckInfor
} from "@/api/system/aCheckInfor";
import {listACheck} from "@/api/system/aCheck";

export default {
  name: "ACheckInfor",
  dicts: ['agent_level', 'check_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打卡详细表格数据
      aCheckInforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chName: null,
        infoContent: null,
        infoDetails: null,
        agentLevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        infoContent: [
          {required: true, message: "分享内容不能为空", trigger: "blur"}
        ],
        infoImage: [
          {required: true, message: "图片展示不能为空", trigger: "blur"}
        ],
        infoDetails: [
          {required: true, message: "详情不能为空", trigger: "blur"}
        ],
        isState: [
          {required: true, message: "权限不能为空", trigger: "change"}
        ],
        agentLevel: [
          {required: true, message: "权限不能为空", trigger: "change"}
        ],
        isShow: [
          {required: true, message: "是否上线不能为空", trigger: "change"}
        ],
        numSoft: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ],
        checkId: [
          {required: true, message: "分类不能为空", trigger: 'blur'}
        ],
      },
      dataPicker: [],
      type_list: [],
      updateTable: true,
    };
  },
  created() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    getTypeList() {
      listACheck({pageNum: 1, pageSize: 1000}).then(resp => {
        this.type_list = resp.rows
      })
    },
    handleSwitchChange(row) {
      updateACheckInfor(row).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        } else {
          this.$modal.msgError(res.msg);
        }
      })
    },
    /** 查询打卡详细列表 */
    getList() {
      this.loading = true;
      if (this.dataPicker && this.dataPicker.length && this.dataPicker[0] && this.dataPicker[1]) {
        this.queryParams.params = {}
        this.queryParams.params.startTime = this.dataPicker[0] + " 00:00:00";
        this.queryParams.params.endTime = this.dataPicker[1] + " 23:59:59";
      }
      listACheckInfor(this.queryParams).then(response => {
        this.aCheckInforList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.updateTable = !this.updateTable
      });
    },
    stripHtmlTags(str) {
      return str.replace(/<[^>]*>/g, '');
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        infoContent: null,
        infoImage: null,
        infoDetails: null,
        likeNum: null,
        downloadNum: null,
        collectionNum: null,
        shareNum: null,
        agentLevel: null,
        isState: '1',
        stateView: null,
        checkId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {}
      this.dataPicker = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打卡详细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getACheckInfor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改打卡详细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateACheckInfor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addACheckInfor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打卡详细编号为"' + ids + '"的数据项？').then(function () {
        return delACheckInfor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aCheckInfor/export', {
        ...this.queryParams
      }, `aCheckInfor_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
